<template>
  <n-grid :x-gap="24" :y-gap="8" cols="2 400:4 600:6">
    <n-grid-item v-for="card in cardList" :key="card.id">
      <n-card hoverable @click="toDetail(card)">
        <div @click.stop="handleRemove(card)" class="card-close">x</div>
        <template #header>
          {{ card.name }}
        </template>
        <template #cover>
          <div class="card-cover">
            <img class="cover-img" v-if="card.img[0]" :src="baseImage + card.img[0]" />
            <img class="cover-img" v-else src="@/assets/xu.png" />
          </div>
        </template>
        <n-ellipsis :line-clamp="2" style="--height: 44px;">
          {{ card.textareaList[0].content }}
        </n-ellipsis>
      </n-card>
    </n-grid-item>
  </n-grid>
</template>
<script lang="ts" setup>
  import { NCard, NGrid, NGridItem, NEllipsis, useLoadingBar, NEmpty, NSpace, NSpin } from 'naive-ui'
  import { reactive, ref, inject, onMounted } from "vue"

  import { listCard, delCard } from '@/api/card'

  const cardId = inject('cardId')
  const type = inject('type')

  // 列表加载
  const loadingBar = useLoadingBar()
  // 图片地址
  // const baseImage = 'http://localhost:9000'
  const baseImage = 'http://120.79.73.210:9000'
  const cardList = ref([])

  /** 前往详情页 */
  const toDetail = (card) => {
    cardId.value = card.id
    type.value = 'edit'
  }

  /** 删除卡片 */
  const handleRemove = (card) => {
    delCard(card.id).then(res => {
      getList()
    })
  }

  const getList = () => {
    loadingBar.start()
    listCard().then(res => {
      cardList.value = res.list
      loadingBar.finish()
    })
  }

  onMounted(() => {
    getList()
  })
</script>
<style lang="scss" scoped>
  .n-card {
    position: relative;
    cursor: pointer;
    .card-cover {
      height: 280px;
      @media screen and (max-width: 600px) {
        height: 800px;
      }
      .cover-img {
        width: 100%;
        height: 100%;
        object-fit: contain;
      }
    }
    .card-close {
      display: none;
    }
    &:hover {
      .card-close {
        position: absolute;
        top: -7px;
        right: -5px;
        background: #ff5858;
        border-radius: 50%;
        color: #fff;
        width: 20px;
        height: 20px;
        display: flex;
        justify-content: center;
        align-items: center;
      }
    }
  }
  ::v-deep(.n-ellipsis) {
    height: var(--height);
  }
  
</style>